package cn.itrip.auth.service;
import cn.itrip.utlis.MySmsSDK;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;

/**
 * Created by LENOVO on 2018/8/17.
 */
@Service
public class ThirdPartyServiceImpl implements ThirdPartyService {
    @Resource
    private SimpleMailMessage mailMessage;
    @Resource
    private JavaMailSender mailSender;
    @Resource
    private MySmsSDK restAPI;

    public static String account = "qq18357009004@163.com";

    public static String password = "qq412727";
    /**
     * 发送短信
     * @param phone
     * @param VerificationCode
     */
    @Override
    public void sendSms(String phone, String VerificationCode) {

        CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
        restAPI.init("app.cloopen.com","8883");
        restAPI.setAccount("8a216da86521b65701652904e0e40442", "55ca31219ac04e8493173ecf95f4b1a4");
        restAPI.setAppId("8a216da86521b65701652904e1360448");
        HashMap<String, Object> result = restAPI.sendTemplateSMS(phone,"1" ,new String[]{VerificationCode,"2"});
        System.out.println("SDKTestGetSubAccounts result=" + result);
        if("000000".equals(result.get("statusCode"))){
            HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for(String key:keySet){
                Object object = data.get(key);
                System.out.println(key +" = "+object);
            }
        }
        else
        {
          System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
        }
    }
    /**
     * 发送短信
     * @param phone
     * @param VerificationCode
     */
    @Override
    public void sendSms1(String phone, String VerificationCode) {

        HashMap<String, Object> result = restAPI.sendTemplateSMS(phone,"1" ,new String[]{VerificationCode,"2"});
        if("000000".equals(result.get("statusCode"))){
            HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for(String key:keySet){
                Object object = data.get(key);
                System.out.println(key +" = "+object);
            }
        }
    }

    /**
     * 发送邮件
     * @param receiveMailAccount
     * @param activationCode
     * @throws Exception
     */
    @Override
    public void sendMail(String receiveMailAccount,String activationCode) throws Exception {
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.smtp.host", "smtp.163.com");
        props.setProperty("mail.smtp.auth", "true");
        Session session = Session.getDefaultInstance(props);
        MimeMessage message = createMimeMessage(session, account, receiveMailAccount,activationCode);
        Transport transport = session.getTransport();
        transport.connect(account, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
    /**
     * 发送邮件
     * @param receiveMailAccount
     * @param activationCode
     * @throws Exception
     */
    @Override
    public void sendMail1(String receiveMailAccount,String activationCode) throws Exception {
        mailMessage.setTo(receiveMailAccount);
        mailMessage.setText("注册邮箱："+receiveMailAccount +"  激活码："+activationCode);
        mailSender.send(mailMessage);
    }

    /**
     * 邮件信息
     * @param session
     * @param sendMail
     * @param receiveMail
     * @param activationCode
     * @return
     * @throws Exception
     */
    private MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail,String activationCode)
            throws Exception {
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(sendMail, "爱旅行", "UTF-8"));
        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "爱旅行用户", "UTF-8"));
        message.setSubject("爱旅行注册激活", "UTF-8");
        message.setContent("您的激活码："+ activationCode, "text/html;charset=UTF-8");
        message.setSentDate(new Date());
        message.saveChanges();
        return message;
    }
}
